/**
 * Mark Fedor
 * Tony Greseth
 * CS 5551 User Interface Design
 * Section 1
 * Sai Chittilla
 * Yahtzee Program
 */
package Yahtzee;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
 * The Die class holds general data about a die in the Dice class
 * and can randomly generate a die value
 */
public class Die extends JComponent implements MouseListener {

    /**
     * constructs a Die and sets its default value to 1
     */
    public Die()
    {
        // Preferred size is set, but layout may change it.
        setPreferredSize(new Dimension(60,60));
        dieValue = 1;

    }

    /**
     * randomly generates an int between 1-6 and sets it to the dieValue
     * does not change value if locked == true
     * @return Result of roll (1-6)
     */
    public void roll()
    {
        if (locked == false)
        {
            int val = (int)(6*Math.random() + 1);   // Range 1-6
            setValue(val);
        }
    }

    /**
     * accessor returns the dieValue
     * @return int representing dieValue
     */
    public int getValue()
    {
        return dieValue;
    }
    /** Sets the value of the Die.  Causes repaint().
     * @param spots Number from 1-6.
     */
    public void setValue(int spots) {
        dieValue = spots;
        repaint();    // Value has changed, must repaint
    }


    /**
     * accessor returns locked
     * @return boolean returns locked
     */
    public boolean isLocked()
    {
        return locked;
    }

    /**
     * mutator sets locked
     * @param setLock sets locked as parameter
     */
    public void setLocked(boolean setLock)
    {
        locked = setLock;
    }
    /**
     * Draws spots of die face, overrided paintcomponent from jcomponent class
     * @param g is a graphics object
     */
    public void paintComponent(Graphics g) {
        int w = getWidth();  // Get height and width
        int h = getHeight();

        // Change to Graphic2D for smoother spots.
        Graphics2D g2 = (Graphics2D)g; 
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        // Paint background
        if (isLocked())
        {
            g2.setColor(Color.red);
        }
        else
        {
            g2.setColor(Color.WHITE);
        }
        g2.fillRect(0, 0, w, h);
        g2.setColor(Color.BLACK);

        g2.drawRect(0, 0, w-1, h-1);  // Draw border

        switch (dieValue) {
            case 1:
                drawSpot(g2, w/2, h/2);
                break;
            case 3:
                drawSpot(g2, w/2, h/2);
                // Fall thru to next case
            case 2:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                break;
            case 5:
                drawSpot(g2, w/2, h/2);
                // Fall thru to next case
            case 4:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                drawSpot(g2, 3*w/4, h/4);
                drawSpot(g2, w/4, 3*h/4);
                break;
            case 6:
                drawSpot(g2, w/4, h/4);
                drawSpot(g2, 3*w/4, 3*h/4);
                drawSpot(g2, 3*w/4, h/4);
                drawSpot(g2, w/4, 3*h/4);
                drawSpot(g2, w/4, h/2);
                drawSpot(g2, 3*w/4, h/2);
                break;
        }
    }

    // Utility method used by paintComponent()
    private void drawSpot(Graphics2D g2, int x, int y) {
        g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
    }

    

    private int dieValue;
    private boolean locked;
    private static final int SPOT_DIAM = 9;  // Diameter of spots

    /**
     * allows the user to lock or unlock the die by clicking on the die
     * @param e just the MouseEvent
     */
    public void mouseClicked(MouseEvent e) {
        if (!GUI.initialRoll)
        {
        if (locked)
            locked = false;
        else
            locked = true;
        repaint();
        }
    }

    // these methods are included in order for the class to implement
    // MouseListener

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

} // end Die Class
